package com.mililoan.common.utils;

public class EncryptUtil {
    private static final String HEAD="H@";
    private static final long KEY = 17;
    private static final String S_KEY = String.valueOf(KEY);
    public static String encrypt(long a) {
        boolean zero = false;
        String res;
        long i = Long.valueOf(S_KEY + a);
        i = i << KEY;
        if (String.valueOf(i).endsWith("0")) {
            zero = true;
            res = String.valueOf(i);
        } else {
            res = reverseString(String.valueOf(i));
        }
        res = reverseString(Long.toHexString(Long.valueOf(res)));
        if (zero) {
            res = "Z" + res;
        }
        res=HEAD+res;
        return res;
    }
    public static String decrypt(String a) {
        if (a.startsWith(HEAD)) {
            a = a.substring(2, a.length());
        }else{
            return null;
        }
        boolean zero = false;
        if (a.startsWith("Z")) {
            zero = true;
            a = a.substring(1, a.length());
        }
        a = reverseString(a);
        long l = Long.valueOf(a, 16);
        if (zero) {
            l = Long.valueOf(String.valueOf(l));
        } else {
            l = Long.valueOf(reverseString(String.valueOf(l)));
        }
        String res = String.valueOf(l >> KEY);
        return res.substring(S_KEY.length(), res.length());
    }
    public static String reverseString(String str) {
        StringBuffer stringBuffer = new StringBuffer(str);
        return (stringBuffer.reverse()).toString();
    }
    public static void main(String[] args) {
        String res;
        for (long i = 0; i < 1000; i++) {
            res = encrypt(i);
            System.out.println("res=" + res);
            if (Long.parseLong(decrypt(res)) != i) {
                System.out.println("res=" + res);
            }
        }
    }
}